home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / filetable.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  14.1 KB  |  397 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Authors: Don Welch
  20. #
  21.  
  22.  
  23. # Std Lib
  24. import sys
  25. import os.path
  26. import os
  27.  
  28. # Local
  29. from base.g import *
  30. from base import utils, magic
  31. from prnt import cups
  32. from base.codes import *
  33. from ui_utils import *
  34.  
  35. # Qt
  36. from PyQt4.QtCore import *
  37. from PyQt4.QtGui import *
  38.  
  39. # Other UI
  40. from mimetypesdialog import MimeTypesDialog
  41.  
  42.  
  43. FILETABLE_TYPE_PRINT = 0
  44. FILETABLE_TYPE_FAX = 1
  45.  
  46.  
  47.  
  48. class FileTable(QWidget):
  49.     def __init__(self, parent):
  50.         QWidget.__init__(self, parent)
  51.         self.parent = parent
  52.         self.working_dir = user_conf.workingDirectory()
  53.         self.initUi()
  54.         self.file_list = []
  55.         self.typ = FILETABLE_TYPE_PRINT
  56.         self.selected_filename = None
  57.         self.fax_add_callback = None
  58.         self.allowable_mime_types = cups.getAllowableMIMETypes()
  59.  
  60.  
  61.     def initUi(self):
  62.         self.gridlayout = QGridLayout(self)
  63.         self.gridlayout.setObjectName("gridlayout")
  64.         self.FileTable = QTableWidget(self)
  65.         self.FileTable.setObjectName("FileTable")
  66.         self.gridlayout.addWidget(self.FileTable,0,0,1,6)
  67.         self.AddFileButton = QPushButton(self)
  68.         self.AddFileButton.setObjectName("AddFileButton")
  69.         self.gridlayout.addWidget(self.AddFileButton,1,0,1,1)
  70.         self.RemoveFileButton = QPushButton(self)
  71.         self.RemoveFileButton.setObjectName("RemoveFileButton")
  72.         self.gridlayout.addWidget(self.RemoveFileButton,1,1,1,1)
  73.         self.MoveFileUpButton = QPushButton(self)
  74.         self.MoveFileUpButton.setObjectName("MoveFileUpButton")
  75.         self.gridlayout.addWidget(self.MoveFileUpButton,1,2,1,1)
  76.         self.MoveFileDownButton = QPushButton(self)
  77.         self.MoveFileDownButton.setObjectName("MoveFileDownButton")
  78.         self.gridlayout.addWidget(self.MoveFileDownButton,1,3,1,1)
  79.         spacerItem = QSpacerItem(91,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  80.         self.gridlayout.addItem(spacerItem,1,4,1,1)
  81.         self.ShowTypesButton = QPushButton(self)
  82.         self.ShowTypesButton.setObjectName("ShowTypesButton")
  83.         self.gridlayout.addWidget(self.ShowTypesButton,1,5,1,1)
  84.         self.AddFileButton.setText(self.__tr("Add..."))
  85.         self.AddFileButton.setIcon(QIcon(load_pixmap('list_add', '16x16')))
  86.         self.connect(self.AddFileButton, SIGNAL("clicked()"), self.AddFileButton_clicked)
  87.         self.RemoveFileButton.setIcon(QIcon(load_pixmap('list_remove', '16x16')))
  88.         self.RemoveFileButton.setText(self.__tr("Remove"))
  89.         self.connect(self.RemoveFileButton, SIGNAL("clicked()"), self.RemoveFileButton_clicked)
  90.         self.MoveFileUpButton.setText(self.__tr("Move Up"))
  91.         self.MoveFileUpButton.setIcon(QIcon(load_pixmap('up', '16x16')))
  92.         self.connect(self.MoveFileUpButton, SIGNAL("clicked()"), self.MoveFileUpButton_clicked)
  93.         self.MoveFileDownButton.setText(self.__tr("Move Down"))
  94.         self.MoveFileDownButton.setIcon(QIcon(load_pixmap('down', '16x16')))
  95.         self.connect(self.MoveFileDownButton, SIGNAL("clicked()"), self.MoveFileDownButton_clicked)
  96.         self.ShowTypesButton.setText(self.__tr("Show Valid Types..."))
  97.         self.ShowTypesButton.setIcon(QIcon(load_pixmap('mimetypes', '16x16')))
  98.         self.connect(self.ShowTypesButton, SIGNAL("clicked()"), self.ShowTypesButton_clicked)
  99.         self.FileTable.setContextMenuPolicy(Qt.CustomContextMenu)
  100.         self.connect(self.FileTable, SIGNAL("customContextMenuRequested(const QPoint &)"),
  101.             self.FileTable_customContextMenuRequested)
  102.         self.headers = [self.__tr("Name"), self.__tr("Type"), self.__tr("Folder/Path")]
  103.  
  104.         self.FileTable.setSortingEnabled(False)
  105.         self.connect(self.FileTable, SIGNAL("itemSelectionChanged()"), self.FileTable_itemSelectionChanged)
  106.  
  107.  
  108.     def setWorkingDir(self, d):
  109.         if os.path.exists(d):
  110.             self.working_dir = d
  111.  
  112.  
  113.     def getWorkingDir(self):
  114.         if self.file_list:
  115.             self.working_dir = os.path.pathname(self.file_list[0][0])
  116.             user_conf.setWorkingDirectory(self.working_dir)
  117.  
  118.         return self.working_dir
  119.  
  120.  
  121.     def setType(self, t):
  122.         self.typ = t
  123.         if self.typ == FILETABLE_TYPE_FAX:
  124.             self.headers = [self.__tr("Name"), self.__tr("Type"), self.__tr("Pages")]
  125.             if log.is_debug():
  126.                 self.headers.append(self.__tr("File"))
  127.  
  128.  
  129.     def setFaxCallback(self, callback):
  130.         self.fax_add_callback = callback
  131.  
  132.  
  133.     def isNotEmpty(self):
  134.         return len(self.file_list)
  135.  
  136.  
  137.     def FileTable_itemSelectionChanged(self):
  138.         self.selected_filename = self.currentFilename()
  139.         self.setUpDownButtons()
  140.  
  141.  
  142.     def updateUi(self, show_add_file_if_empty=True):
  143.         self.FileTable.clear()
  144.         self.FileTable.setRowCount(len(self.file_list))
  145.         self.FileTable.setColumnCount(0)
  146.  
  147.         if self.file_list:
  148.             self.emit(SIGNAL("isNotEmpty"))
  149.             QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  150.             try:
  151.                 selected = None
  152.                 self.FileTable.setColumnCount(len(self.headers))
  153.                 self.FileTable.setHorizontalHeaderLabels(self.headers)
  154.                 flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  155.  
  156.                 for row, f in enumerate(self.file_list):
  157.                     filename, mime_type, mime_type_desc, title, num_pages = f
  158.                     col = 0
  159.  
  160.                     if self.typ == FILETABLE_TYPE_FAX:
  161.                         if title:
  162.                             i = QTableWidgetItem(title)
  163.                         else:
  164.                             i = QTableWidgetItem(os.path.basename(filename))
  165.                     else: # FILETABLE_TYPE_PRINT
  166.                         # Filename (basename)
  167.                         i = QTableWidgetItem(os.path.basename(filename))
  168.  
  169.                     i.setData(Qt.UserRole, QVariant(filename))
  170.                     i.setFlags(flags)
  171.  
  172.                     if self.selected_filename is not None and \
  173.                         self.selected_filename == filename:
  174.                         selected = i
  175.  
  176.                     self.FileTable.setItem(row, col, i)
  177.                     col += 1
  178.  
  179.                     # MIME type
  180.                     i = QTableWidgetItem(mime_type_desc)
  181.                     i.setFlags(flags)
  182.                     self.FileTable.setItem(row, col, i)
  183.                     col += 1
  184.  
  185.                     if self.typ == FILETABLE_TYPE_PRINT:
  186.                         # path/folder
  187.                         i = QTableWidgetItem(os.path.dirname(filename))
  188.                         i.setFlags(flags)
  189.                         self.FileTable.setItem(row, col, i)
  190.                         col += 1
  191.  
  192.                     if self.typ == FILETABLE_TYPE_FAX:
  193.                         # num pages
  194.                         if num_pages < 1:
  195.                             i = QTableWidgetItem(self.__tr("(unknown)"))
  196.                         else:
  197.                             i = QTableWidgetItem(unicode(num_pages))
  198.                         i.setFlags(flags)
  199.                         self.FileTable.setItem(row, col, i)
  200.                         col += 1
  201.  
  202.                         if self.typ == FILETABLE_TYPE_FAX and log.is_debug():
  203.                             i = QTableWidgetItem(filename)
  204.                             i.setFlags(flags)
  205.                             self.FileTable.setItem(row, col, i)
  206.  
  207.  
  208.                 self.FileTable.resizeColumnsToContents()
  209.  
  210.                 if selected is None:
  211.                     selected = self.FileTable.item(0, 0)
  212.  
  213.                 selected.setSelected(True)
  214.                 self.FileTable.setCurrentItem(selected)
  215.  
  216.             finally:
  217.                 QApplication.restoreOverrideCursor()
  218.  
  219.             self.RemoveFileButton.setEnabled(True)
  220.             self.RemoveFileButton.setIcon(QIcon(load_pixmap('list_remove', '16x16')))
  221.  
  222.             self.setUpDownButtons()
  223.  
  224.         else:
  225.             self.emit(SIGNAL("isEmpty"))
  226.             self.RemoveFileButton.setEnabled(False)
  227.             self.setUpDownButtons()
  228.  
  229.             if show_add_file_if_empty:
  230.                 self.AddFileButton.emit(SIGNAL("clicked()"))
  231.  
  232.  
  233.     def setUpDownButtons(self):
  234.         if self.file_list:
  235.             i = self.FileTable.currentRow()
  236.  
  237.             if len(self.file_list) > 1 and i != len(self.file_list)-1:
  238.                 self.MoveFileDownButton.setEnabled(True)
  239.             else:
  240.                 self.MoveFileDownButton.setEnabled(False)
  241.  
  242.             if len(self.file_list) > 1 and i != 0:
  243.                 self.MoveFileUpButton.setEnabled(True)
  244.             else:
  245.                 self.MoveFileUpButton.setEnabled(False)
  246.  
  247.         else:
  248.             self.MoveFileDownButton.setEnabled(False)
  249.             self.MoveFileUpButton.setEnabled(False)
  250.  
  251.  
  252.     def AddFileButton_clicked(self):
  253.         if self.typ == FILETABLE_TYPE_PRINT:
  254.             s = self.__tr("Select File(s) to Print")
  255.         else:
  256.             s = self.__tr("Select File(s) to Send")
  257.  
  258.         files = list(QFileDialog.getOpenFileNames(self, s,
  259.             self.working_dir, self.__tr("All files (*)")))
  260.  
  261.         files = [unicode(f) for f in files]
  262.  
  263.         if files:
  264.             self.addFileList(files)
  265.  
  266.             if self.typ == FILETABLE_TYPE_PRINT:
  267.                 self.updateUi(False)
  268.  
  269.  
  270.     def addFileList(self, file_list):
  271.         for f in file_list:
  272.             if self.typ == FILETABLE_TYPE_PRINT:
  273.                 self.addFileFromUI(f)
  274.             else:
  275.                 self.fax_add_callback(f)
  276.  
  277.  
  278.     def addFileFromUI(self, f, title='', num_pages=0):
  279.         f = os.path.abspath(os.path.expanduser(f))
  280.         log.debug("Trying to add file: %s" % f)
  281.  
  282.         if os.path.exists(f) and os.access(f, os.R_OK):
  283.             mime_type = magic.mime_type(f)
  284.             mime_type_desc = mime_type
  285.             log.debug("File type of file %s: %s" % (f, mime_type))
  286.  
  287.             try:
  288.                 mime_type_desc = MIME_TYPES_DESC[mime_type][0]
  289.             except KeyError:
  290.                 if self.typ == FILETABLE_TYPE_PRINT:
  291.                     FailureUI(self, self.__tr("<b>You are trying to add a file '%1' that cannot be directly printed with this utility.</b><p>To print this file, use the print command in the application that created it.<p>Note: Click <i>Show Valid Types...</i> to view a list of compatible file types that can be directly printed from this utility.").arg(f),
  292.                         self.__tr("HP Device Manager"))
  293.                 else:
  294.                     FailureUI(self, self.__tr("<b>You are trying to add a file '%1' that cannot be directly faxed with this utility.</b><p>To fax this file, use the print command in the application that created it (using the appropriate fax print queue).<p>Note: Click <i>Show Valid Types...</i> to view a list of compatible file types that can be directly added to the fax file list in this utility.").arg(f),
  295.                         self.__tr("HP Device Manager"))
  296.             else:
  297.                 self.addFile(f, mime_type, mime_type_desc, title, num_pages)
  298.         else:
  299.             FailureUI(self, self.__tr("<b>Unable to add file '%1' to file list (file not found or insufficient permissions).</b><p>Check the file name and try again.").arg(f),
  300.                 self.__tr("HP Device Manager"))
  301.  
  302.  
  303.     def addFile(self, f, mime_type, mime_type_desc, title, num_pages):
  304.         log.debug("Adding file %s (%s,%s,%s,%d)" % (f, mime_type, mime_type_desc, title, num_pages))
  305.         self.file_list.append((f, mime_type, mime_type_desc, title, num_pages))
  306.         self.updateUi()
  307.         self.emit(SIGNAL("fileListChanged"))
  308.  
  309.  
  310.     def currentFilename(self):
  311.         i = self.FileTable.item(self.FileTable.currentRow(), 0)
  312.         if i is None:
  313.             return None
  314.         return i.data(Qt.UserRole).toString()
  315.  
  316.  
  317.     def RemoveFileButton_clicked(self):
  318.         filename = self.currentFilename()
  319.         if filename is None:
  320.             return
  321.  
  322.         return self.removeFile(filename)
  323.  
  324.  
  325.     def removeFile(self, filename):
  326.         temp = self.file_list[:]
  327.         index = 0
  328.         for f, mime_type, mime_type_desc, title, num_pages in temp:
  329.             if f == filename:
  330.                 del self.file_list[index]
  331.                 self.emit(SIGNAL("fileListChanged"))
  332.                 self.updateUi(False)
  333.                 break
  334.             index += 1
  335.  
  336.  
  337.     def removeFileByMIMEType(self, mime_type):
  338.         temp = self.file_list[:]
  339.         index = 0
  340.         for filename, m, mime_type_desc, title, num_pages in temp:
  341.             if m == mime_type:
  342.                 del self.file_list[index]
  343.                 self.emit(SIGNAL("fileListChanged"))
  344.                 self.updateUi(False)
  345.                 break
  346.             index += 1
  347.  
  348.  
  349.     def isMIMETypeInList(self, mime_type):
  350.         for filename, m, mime_type_desc, title, num_pages in self.file_list:
  351.             if m == mime_type:
  352.                 return True
  353.  
  354.         return False
  355.  
  356.  
  357.     def ShowTypesButton_clicked(self):
  358.         x = {}
  359.         for a in self.allowable_mime_types:
  360.             x[a] = MIME_TYPES_DESC.get(a, ('Unknown', 'n/a'))
  361.  
  362.         dlg = MimeTypesDialog(x, self)
  363.         dlg.exec_()
  364.  
  365.  
  366.     def MoveFileUpButton_clicked(self):
  367.         filename = self.currentFilename()
  368.         if filename is None:
  369.             return
  370.  
  371.         utils.list_move_up(self.file_list, filename, self.__compareFilenames)
  372.         self.updateUi()
  373.  
  374.  
  375.     def MoveFileDownButton_clicked(self):
  376.         filename = self.currentFilename()
  377.         if filename is None:
  378.             return
  379.  
  380.         utils.list_move_down(self.file_list, filename, self.__compareFilenames)
  381.         self.updateUi()
  382.  
  383.  
  384.     def __compareFilenames(self, a, b):
  385.         return a[0] == b
  386.  
  387.  
  388.     def FileTable_customContextMenuRequested(self, p):
  389.         print p
  390.  
  391.  
  392.     def __tr(self,s,c = None):
  393.         return qApp.translate("FileTable",s,c)
  394.  
  395.  
  396.  
  397.